<?

//Data loading
if (GetParam("m","")=="AUDIT")
    $title='TRIAL BALANCE (AUDIT)';
else
    $title='TRIAL BALANCE';
if (GetParam("office","")) $perush = GetCompany (GetParam("office",""));
else $perush = 'PT. MASAJI TATANAN CONTAINER';

$page   = 'PAGE';

$DEFINE_FONT=0;
$pdf=new PDFReport('P',"mm","Letter");
$pdf->AddPage();
$Result = GridList ();
if ($Result) $pdf->GridTable($Result);
//$pdf->Output();

function GetFromDate () {
    if (GetParam("m","")=="AUDIT")
    list($Day,$Month,$Year,$NMonth) = explode("-",date("d-m-Y-F",mktime(0,0,0,12,1,GetParam("y",""))));
    else
    list($Day,$Month,$Year,$NMonth) = explode("-",date("d-m-Y-F",mktime(0,0,0,GetParam("m",""),1,GetParam("y",""))));
    return array($Day,$Month,$Year,$NMonth);
}

function GetLastPeriod ($Month, $Year) {
    list($LDay,$LMonth,$LYear,$LNMonth) = explode("-",date("d-m-Y-F",mktime(0,0,0,$Month,0,$Year)));
    return array($LMonth,$LYear,$LNMonth);
}

function GetLastBl() {
            global $DBConnection;
            list($Day,$Month,$Year,$NMonth) = GetFromDate();
            list($LMonth,$LYear,$LNMonth) = GetLastPeriod ($Month, $Year);

            //Balance Last Month

            //Rama, (16 November 2009), Start
            if(GetParam("m","")=="AUDIT") {
                list($LMonth,$LYear,$LNMonth) = GetLastPeriod ($Month+1, $Year);
            }
            //Rama, (16 November 2009), End


            //if ($LMonth==12) $modcondition = " awal";
            //Rama, (16 November 2009), Start
            if ($LMonth==12 && GetParam("m","")<>"AUDIT") $modcondition = " awal";
            //Rama, (16 November 2009), End
            else $modcondition = " aku$LMonth";
            $SQL = "SELECT sum($modcondition) FROM tblcoa WHERE ledger='$Year' AND skada<>'".Get_salrlytd()."' ";
            return $DBConnection->dbc->get_var($SQL);
}


function GridList () {
            global $DBConnection;

            list($Day,$Month,$Year,$NMonth) = GetFromDate();
            list($LMonth,$LYear,$LNMonth) = GetLastPeriod ($Month, $Year);
            //Balance Last Month
                        //$TEMP = "TEMPORARY";

                                    //Rama, (16 November 2009), Start
            if(GetParam("m","")=="AUDIT") {
                list($LMonth,$LYear,$LNMonth) = GetLastPeriod ($Month+1, $Year);
            }
            //Rama, (16 November 2009), End

            //if ($LMonth==12) $modcondition = " ,awal as lastbl ";
            //Rama, (16 November 2009), Start
            if ($LMonth==12  && GetParam("m","")<>"AUDIT") $modcondition = " ,awal as lastbl ";
            //Rama, (16 November 2009), End
            else $modcondition = " ,aku$LMonth as lastbl ";

                        $SQL = "DROP TABLE IF EXISTS `ais_tmpmemo`";
                        $DBConnection->dbc->query($SQL);

            $SQL =  "CREATE $TEMP TABLE `ais_tmpmemo` (".
                    "`skada` VARCHAR( 64 ) NOT NULL ,".
                    "`tglbkt` DATE NOT NULL ,".
                    "`debet` DECIMAL( 15, 2 ) NOT NULL ,".
                    "`credit` DECIMAL( 15, 2 ) NOT NULL,".
                    "`ktrasl` VARCHAR( 64 ) NOT NULL ) ";
            $DBConnection->dbc->query($SQL);
            //print $SQL;die();

                $SQL = "INSERT INTO `ais_tmpmemo` (skada, tglbkt, debet, credit, ktrasl)
                        SELECT skada, tglbkt, sum(IF(dk='D',rupiah,0)) as debet, sum(IF(dk='K',rupiah,0)) as credit, ktrasl
                        FROM ais_memorial
                        WHERE  MONTH(tglbkt)=".($Month*1)." AND YEAR(tglbkt)=$Year
                       ";

                if (GetParam("office",""))
                $SQL .= " AND ktrasl='".GetParam("office","")."'";

                if (GetParam("m","")=="AUDIT")
                //dyah.110809
                //$SQL .= " AND nobkt like 'MA%'";
                $SQL .= " AND (nobkt like 'MA%' OR nobkt REGEXP '^MM[0-9]{2}13')";
                $SQL .= " GROUP BY ais_memorial.skada,ais_memorial.ktrasl";

            $DBConnection->dbc->query($SQL);
            //print $SQL;die();

            $SQL = "SELECT tblcoa.ktrasl, tblcoa.skada, tblcoa.namacc, ais_tmpmemo.debet, ais_tmpmemo.credit $modcondition
                    FROM tblcoa ";
                        if (!GetParam("office","")) {
                              $SQL .= "LEFT JOIN ais_tmpmemo ON tblcoa.skada=ais_tmpmemo.skada AND tblcoa.ktrasl=ais_tmpmemo.ktrasl ";
                        } else {
                              $SQL .= "LEFT JOIN ais_tmpmemo ON tblcoa.skada=ais_tmpmemo.skada ";
                        }
                        $SQL .= " WHERE tipe=0 $WHERE AND tblcoa.skada<>'".Get_salrlytd()."' AND tblcoa.ledger='$Year' ";

                        if (GetParam("office","")) {
                              $SQL .= " AND tblcoa.ktrasl='".GetParam("office","")."'";
                        }
                        $SQL .= " ORDER BY tblcoa.ktrasl, tblcoa.skada";
			
			
			//MODIFICATION AND NOT REAL  
             	
                /*$SQL = "SELECT tblcoa.ktrasl, tblcoa.skada, tblcoa.namacc, tblcoa.$mutd as debet, tblcoa.$mutk as credit $modcondition
                    FROM tblcoa ";
                        $SQL .= " WHERE 1 $WHERE AND tblcoa.skada<>'".Get_salrlytd()."' AND tblcoa.ledger='$Year' AND length(tblcoa.skada)=8";

                        if (GetParam("office","")) {
                              $SQL .= " AND tblcoa.ktrasl='".GetParam("office","")."'";
                        }

                        $SQL .= " ORDER BY tblcoa.skada";*/
             
            ///echo "$SQL"; exit;
			
			
            $MyResult = $DBConnection->dbc->get_results($SQL,ARRAY_A);
            return $MyResult;
}

function Get_salrlytd() {
   global $DBConnection;
  $SQL = "SELECT salrlytd FROM tblprofile";
  return $DBConnection->dbc->get_var($SQL);
}

function BuildPeriod ($year, $month) {
  $arraymonth = array("","JANUARI","FEBRUARI","MARET","APRIL","MEI","JUNI","JULI","AGUSTUS","SEPTEMBER","OKTOBER","NOPEMBER","DESEMBER");
  return $arraymonth[$month].", $year";
}

class PDFReport extends PDF {

    function Header() {
        global $title,$perush,$page,$DEFINE_FONT;
        if ($DEFINE_FONT==0) {
            $this->AddFont('Arial','','arialn.php');
            $DEFINE_FONT=1;
        }

        list($Day,$Month,$Year,$NMonth) = GetFromDate();
        list($LMonth,$LYear,$LNMonth) = GetLastPeriod ($Month, $Year);


            //Rama, (07 September 2009), Start
            if (GetParam("m","")=="AUDIT"){
                $LNMonth = "DECEMBER";
                $NMonth = "THIRTEEN MONTH";
            }
            //Rama, (07 September 2009), End

        $this->SetFont('Arial','',10);
        $this->Cell(180,5,$perush,0,1,'C',0);
        $this->Cell(180,5,$title,0,1,'C',0);
        if (GetParam("m","")=="AUDIT")
        $this->Cell(180,5,"PERIOD: ".BuildPeriod (GetParam("y",""), 12),0,1,'C',0);
        else
        $this->Cell(180,5,"PERIOD: ".BuildPeriod (GetParam("y",""), GetParam("m","")),0,1,'C',0);
        $this->Cell(180,5,$page." ".$this->PageNo(),0,1,'R',0);
//      $this->Ln();
        //Header

        //Column widths
        $w=array(80,25,25,25,25);
        $this->SetFont('Arial','',8);
        $this->Cell($w[0],10,"TR. ACCOUNT","LRBT",0,'C',0);
        $this->Cell($w[1],5,"BALANCE","LRT",0,'C',0);
        $this->Cell($w[2]+$w[3],5,strtoupper($NMonth),"LRT",0,'C',0);
//      $this->Cell($w[3],5,"","TR",0,'C',0);
        $this->Cell($w[4],5,"BALANCE","LRT",0,'C',0);
        $this->Ln();

        $this->Cell($w[0],5,"","LR",0,'C',0);
        $this->Cell($w[1],5,strtoupper($LNMonth),"LRB",0,'C',0);
        $this->Cell($w[2],5,"DEBET",1,0,'C',0);
        $this->Cell($w[3],5,"CREDIT",1,0,'C',0);
        $this->Cell($w[4],5,strtoupper($NMonth),"LRB",0,'C',0);
        $this->Ln();

    }

    function GridTable($MyResult) {
        //Column widths
        $w=array(80,25,25,25,25);

        //Data
        $GridString = "";
        foreach($MyResult as $row)
        {
                $totaldebet = $totaldebet + $row["debet"];
                $totalcredit = $totalcredit + $row["credit"];
            $totallastbl = $totallastbl + $row["lastbl"];
            if (!$row["debet"]) $row["debet"]=0;
            if (!$row["credit"]) $row["credit"]=0;
			if ($row["debet"]<>0 || $row["credit"]<>0 || $row["lastbl"]<>0) 
			{
	            $GridString .= "'".$row["ktrasl"]."\t";
	            $GridString .= "'".$row["skada"]."\t";
	            $GridString .= "\"".addslashes($row["namacc"])."\"\t";
	            $GridString .= "\"".$row["lastbl"]."\"\t";
	            $GridString .= "\"".$row["debet"]."\"\t";
	            $GridString .= "\"".$row["credit"]."\"\t";
	            $Currmonth = (($row["lastbl"]+$row["debet"])-$row["credit"]);
	            $GridString .= "'".$Currmonth."\n";
	            //$GridString .= "\"".$row["lastbl"]+$row["debet"]-$row["credit"]."\n";
        	}
        }
            header("Content-type:application/vnd.ms-excel");
            header("Content-Disposition:filename=trial_balance_excel.xls");
            print $GridString;
            exit();
        //Closure line
    }

    function Footer() {
        //Closure line
        $this->Cell(180,0,'','T');
    }

}


?>
